﻿$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

var getValueByPath = function (data, path) {

    var val = null;

    $.each(path.toString().split("."), function (index, r) {
        if (val != undefined)
            val = val[r];
        else
            val = data[r];
    });

    return val;

};

var Office = function (config) {

    return {

        Store : [],

        Form: {
            load: function (config) {

                config.renderTo.find("*").each(function (i, r) {

                    var attr = $(r).attr("name");

                    if (typeof attr !== 'undefined' && attr !== false) {

                        $(config.data).each(function (i1, r1) {
                            var val = null;

                            $.each(attr.toString().split("."), function (index, i2) {
                                if (val != undefined)
                                    val = val[i2]
                                else
                                    val = config.data[i2]
                            });

                            if (val != null)
                                $(r).val(val);

                        });
                    }

                });

                /*
                for (var i in config.data) {
                    if (config.renderTo) {
                        config.renderTo.find("*[name='" + i + "']").val(config.data[i]);
                    } else {
                        $("*[name='" + i + "']").val(config.data[i]);
                    }
                }*/
            }
        }

        , Grid: function (config) {

            var header = [];
            var table = $("table[data-name='" + config.tableName + "']");
            var body = table.find("tbody");

            body.empty();

            table.find("th").each(function (i, r) {
                header.push($(r).attr("data-name"));
            });

            $(config.data).each(function (i, r) {

                var tr = $("<tr>");

                if (config.index) {
                    tr.attr("data-id", r[config.index]);
                } else {
                    tr.attr("data-id", r["GUID_RECORD"]);
                }

                $.each(header, function (index, h) {
                    
                    var td = $("<td>");
                    var val = r;

                    $.each(h.toString().split("."), function (index, name) {
                        if (val != undefined)
                            val = val[name];
                    });

                    td.text(val);
                    tr.append(td);


                });

                body.append(tr);
            });

        }

        , RenderGrid: function (config) {

            if (!config.renderTo)
                config.renderTo = $("<table>");

            config.renderTo.empty();

            for (var r in config.data) {

                var tr = $("<tr>");
                tr.attr("data-id", config.data[r]["GUID_RECORD"]);

                for (var f in config.fields) {

                    var td = $("<td>");
                    var val = config.data[r];

                    $.each(config.fields[f].toString().split("."), function (index, i) {
                        val = val[i];
                    });

                    td.text(val);
                    tr.append(td);
                }

                //if (config.subfields) {
                //    var td = $("<td>");
                    
                //    var i = $("<i>");
                //    i.addClass("icon-download");
                //    i.addClass("toggleSubTable");
                //    i.css("cursor", "pointer");

                //   td.append(i);
                //    tr.append(td);
                //}

                config.renderTo.append(tr);


                if (config.subfields) {

                    var tr = $("<tr>");
                    tr.attr("data-parent-id", config.data[r]["GUID_RECORD"]);
                    tr.hide();

                    var td = $("<td>");
                    td.attr("colspan", config.fields.length);

                    this.RenderGrid({
                        renderTo: td,
                        data: config.data,
                        fields: config.fields
                    });

                    tr.append(td);
                    config.renderTo.append(tr);
                }
            }

            return config.renderTo;
        }

        , RenderGridRow: function (config) {

            config.renderTo.empty();
            for (var f in config.fields) {

                var td = $("<td>");
                td.text(config.data[config.fields[f]]);
                config.renderTo.append(td);
            }
        }
    };
};
